package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.ArticleDto;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/ydd_article")
public class YddArticleController {

    @Autowired
    private IYddArticleService iYddArticleService;
    @Autowired
    private IYddCateService iYddCateService;
    /**
     * 分页查询
     * @param start_time
     * @param end_time
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData page(String start_time,String end_time,String title,String cateid,String page,String limit){
        log.info("page{},limit{}",page,limit);
        //构造分页构造器
        Page<YddArticle> pageInfo = new Page(Integer.parseInt(page),Integer.parseInt(limit));
        //构造条件过滤
        LambdaQueryWrapper<YddArticle> yddArticleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据文章标题进行模糊查询
        yddArticleLambdaQueryWrapper.like(StringUtils.isNotEmpty(title),YddArticle::getTitle,title);
        //根据时间进行查询
        yddArticleLambdaQueryWrapper.between(StringUtils.isNotEmpty(start_time)&&StringUtils.isNotEmpty(end_time),YddArticle::getCreateDate,start_time,end_time);
        //根据分类查询
        if (StringUtils.isNotEmpty(cateid)){
            yddArticleLambdaQueryWrapper.eq(YddArticle::getCateid,Integer.parseInt(cateid));
        }
        //查询
        iYddArticleService.page(pageInfo, yddArticleLambdaQueryWrapper);

        List<ArticleDto> articleDtoList =new ArrayList<>();

        List<YddArticle> articleList = pageInfo.getRecords();
        for (YddArticle yddArticle : articleList) {
            ArticleDto articleDto = new ArticleDto();
            BeanUtils.copyProperties(yddArticle,articleDto);
            YddCate cate = iYddCateService.getById(articleDto.getCateid());
            articleDto.setCatename(cate.getCatename());
            articleDto.setUpdateDate(yddArticle.getUpdateDate());
            articleDto.setCreateDate(yddArticle.getCreateDate());
            articleDtoList.add(articleDto);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("data",articleDtoList);
        map.put("totalPages", pageInfo.getPages());
        map.put("currentPage", pageInfo.getCurrent());
        map.put("pageSize", pageInfo.getSize());
        map.put("count", pageInfo.getTotal());
        return ResultData.ok("成功",map);

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(int id){
        YddArticle article = iYddArticleService.getById(id);
        return ResultData.ok("回显成功",article);
    }


    @PostMapping("/update")
    public ResultData update(@RequestBody YddArticle yddArticle){
        iYddArticleService.updateById(yddArticle);
        return ResultData.ok("",null);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(int id){
        iYddArticleService.removeById(id);
        return ResultData.ok("删除成功",null);
    }

    /**
     * 新增
     * @param yddArticle
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddArticle yddArticle){
        iYddArticleService.save(yddArticle);
        return ResultData.ok("新增成功",null);
    }
}
